#include "formsub.h"
#include "ui_formsub.h"

FormSub::FormSub(int controller_addr, int register_addr, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormSub) {
    ui->setupUi(this);
    // 初始化状态灯
    fcontroller_addr = controller_addr;
    fregister_addr = register_addr;
//    setValus(fregister_addr, 1, 112345, 2, 2, 5);
    qDebug() << QString("初始化 %1 号显示面板.").arg(fregister_addr);
}

FormSub::~FormSub() {
    delete ui;
}

void FormSub::setDescribe(QString str) {
    ui->describe_lab->setText(str);
}

// 将串口数据解码成具体数值
int FormSub::setData(QVector<quint16> values) {
    for(int i = 0, count = values.count(); i < count; i++) {
        ci.data[i] = values[i];  // 正常有11个数据
    }

    setValus(ci.number, ci.name, ci.deepness, ci.unit, ci.piont, ci.state);
    return ci.state;
}

// 设置探测器显示界面数据
void FormSub::setValus(int number, int name, int deepness, int unit, int piont, int state) {
    QString name_str, unit_str; // 气体类型

    float deepness_value = deepness / pow(10, piont);

    // 气体类型
    switch (name) {
    case 0:
        name_str = "甲烷";
        break;
    case 1:
        name_str = "氨气";
        break;
    case 50:
        name_str = "可燃气";
        break;
    default:
        name_str = "";
        break;
    }
    ui->number_name_lab->setText(tr("%1号[%2]").arg(number).arg(name_str));

    // 浓度单位
    switch (unit) {
    case 0x01:
        unit_str = "%lel";
        break;
    case 0x02:
        unit_str = "%vol";
        break;
    case 0x03:
        unit_str = "PPM";
        break;
    default:
        unit_str = "";
        break;
    }
    ui->deepness_unit_lab->setText(tr("%1<span style='font-size:14pt; color:#0080ff;'> %2</span>").arg(deepness_value).arg(unit_str));

    // 状态字节
    switch (state) {
    case 0x00:  // *****预热
        // 正常灯
        ui->normal_led->setStyleSheet("color: rgb(0, 85, 0);\nfont: 20pt \"新宋体\";\n");
        // 预热
        ui->warm_led->setStyleSheet("color: rgb(0, 255, 0);\nfont: 20pt \"新宋体\";\n");
        // 低报
        ui->low_alarm_led->setStyleSheet("color: rgb(85, 0, 0);\nfont: 20pt \"新宋体\";\n");
        // 高报
        ui->high_alarm_led->setStyleSheet("color: rgb(85, 0, 0);\nfont: 20pt \"新宋体\";\n");

        if(low_alarm_state_old) dbSaveAlarm(ui->describe_lab->text(), tr("%1 低报解除.").arg(ui->number_name_lab->text()));
        if(height_alarm_state_old) dbSaveAlarm(ui->describe_lab->text(), tr("%1 高报解除.").arg(ui->number_name_lab->text()));
        if(fault_state_old) dbSaveAlarm(ui->describe_lab->text(), tr("%1 故障解除.").arg(ui->number_name_lab->text()));
        low_alarm_state_old = false, height_alarm_state_old = false, fault_state_old = false;

        break;
    case 0x01:  // *****正常
        // 正常灯
        ui->normal_led->setStyleSheet("color: rgb(0, 255, 0);\nfont: 20pt \"新宋体\";\n");
        // 预热
        ui->warm_led->setStyleSheet("color: rgb(0, 85, 0);\nfont: 20pt \"新宋体\";\n");
        // 低报
        ui->low_alarm_led->setStyleSheet("color: rgb(85, 0, 0);\nfont: 20pt \"新宋体\";\n");
        // 高报
        ui->high_alarm_led->setStyleSheet("color: rgb(85, 0, 0);\nfont: 20pt \"新宋体\";\n");

        if(low_alarm_state_old) dbSaveAlarm(ui->describe_lab->text(), tr("%1 低报解除.").arg(ui->number_name_lab->text()));
        if(height_alarm_state_old) dbSaveAlarm(ui->describe_lab->text(), tr("%1 高报解除.").arg(ui->number_name_lab->text()));
        if(fault_state_old) dbSaveAlarm(ui->describe_lab->text(), tr("%1 故障解除.").arg(ui->number_name_lab->text()));

        low_alarm_state_old = false, height_alarm_state_old = false, fault_state_old = false;

        break;
    case 0x05:  // <<<<<<<<<<*****低报***********
        // 正常灯
        ui->normal_led->setStyleSheet("color: rgb(0, 85, 0);\nfont: 20pt \"新宋体\";\n");
        // 预热
        ui->warm_led->setStyleSheet("color: rgb(0, 85, 0);\nfont: 20pt \"新宋体\";\n");
        // 低报
        ui->low_alarm_led->setStyleSheet("color: rgb(255, 0, 0);\nfont: 20pt \"新宋体\";\n");
        // 高报
        ui->high_alarm_led->setStyleSheet("color: rgb(85, 0, 0);\nfont: 20pt \"新宋体\";\n");

//        if(!low_alarm_state_old)
//            dbSaveAlarm(ui->describe_lab->text(), tr("低报：%1，值：%2").arg(ui->number_name_lab->text()).arg(ui->deepness_unit_lab->text()));

        low_alarm_state_old = true, height_alarm_state_old = false, fault_state_old = false;

        if(low_alarm_value_old != deepness_value) {  // 当低报值有变化，就记录
            dbSaveAlarm(ui->describe_lab->text(), tr("<span style=\"color:#bb9900;\">低报：%1，值：%2%3</span>").arg(ui->number_name_lab->text()).arg(deepness_value).arg(unit_str));
            low_alarm_value_old = deepness_value;
        }

        emit playWarningSound();
        break;
    case 0x06:  //  <<<<<<<<*****高报
        // 正常灯
        ui->normal_led->setStyleSheet("color: rgb(0, 85, 0);\nfont: 20pt \"新宋体\";\n");
        // 预热
        ui->warm_led->setStyleSheet("color: rgb(0, 85, 0);\nfont: 20pt \"新宋体\";\n");
        // 低报
        ui->low_alarm_led->setStyleSheet("color: rgb(85, 0, 0);\nfont: 20pt \"新宋体\";\n");
        // 高报
        ui->high_alarm_led->setStyleSheet("color: rgb(255, 0, 0);\nfont: 20pt \"新宋体\";\n");

//        if(!height_alarm_state_old)
//            dbSaveAlarm(ui->describe_lab->text(), tr("高报：%1，值：%2%3").arg(ui->number_name_lab->text()).arg(deepness_value).arg(unit_str));

        low_alarm_state_old = false, height_alarm_state_old = true, fault_state_old = false;

        if(height_alarm_value_old != deepness_value) {  // 当高报值有变化，就记录
            dbSaveAlarm(ui->describe_lab->text(), tr("<span style=\"color:#aa0000;\">高报：%1，值：%2%3</span>").arg(ui->number_name_lab->text()).arg(deepness_value).arg(unit_str));
            height_alarm_value_old = deepness_value;
        }

        emit playWarningSound();
        break;
    default:    // <<<<<<<<故障
        ui->deepness_unit_lab->setText(tr("故障"));
        // 正常灯
        ui->normal_led->setStyleSheet("color: rgb(0, 85, 0);\nfont: 20pt \"新宋体\";\n");
        // 预热
        ui->warm_led->setStyleSheet("color: rgb(0, 85, 0);\nfont: 20pt \"新宋体\";\n");
        // 低报
        ui->low_alarm_led->setStyleSheet("color: rgb(85, 0, 0);\nfont: 20pt \"新宋体\";\n");
        // 高报
        ui->high_alarm_led->setStyleSheet("color: rgb(85, 0, 0);\nfont: 20pt \"新宋体\";\n");

        if(!fault_state_old)
            dbSaveAlarm(ui->describe_lab->text(), tr("<span style=\"color:#ff0000;\">故障：%1</span>").arg(ui->number_name_lab->text()));

        low_alarm_state_old = false, height_alarm_state_old = false, fault_state_old = true;

        break;
    }

}

void FormSub::setTimeoutState() {
    ui->deepness_unit_lab->setText(tr("超时"));
    // 正常灯
    ui->normal_led->setStyleSheet("color: rgb(0, 85, 0);\nfont: 20pt \"新宋体\";\n");
    // 预热
    ui->warm_led->setStyleSheet("color: rgb(0, 85, 0);\nfont: 20pt \"新宋体\";\n");
    // 低报
    ui->low_alarm_led->setStyleSheet("color: rgb(85, 0, 0);\nfont: 20pt \"新宋体\";\n");
    // 高报
    ui->high_alarm_led->setStyleSheet("color: rgb(85, 0, 0);\nfont: 20pt \"新宋体\";\n");
}

void FormSub::dbSaveAlarm(QString desp, QString memo) {
    QDateTime dateTime = QDateTime::currentDateTime();
    QString datetime_str = dateTime .toString("yyyy-MM-dd hh:mm:ss");//格式化时间
    QSqlQuery query;
    QString sqlstr = QString("insert into alarm(numtime, controller_addr, register_addr, desp, memo) "
                             "values('%1', %2, '%3', '%4', '%5')").arg(datetime_str).arg(fcontroller_addr).arg(fregister_addr+1).arg(desp).arg(memo);
    query.exec(sqlstr);
    qDebug() << sqlstr;
    emit alarmDataChanged();
}


